/*
 * CChannel.h
 *
 *  Created on: 2012-7-17
 *      Author: guanlei
 */

#ifndef CCHANNEL_H_
#define CCHANNEL_H_

#include "IChannel.h"
#include "Message.h"

class CChannel:public IChannel {
	public:
		CChannel(CHANNEL_ID,const char*);
		virtual ~CChannel();
		//添加新的用户到聊天室
		virtual void	AddChater(IChater*);
		//根据用户移除用户到聊天室
		virtual void	RemoveChaterById(CHATER_ID);
		virtual void	RemoveChater(IChater*);
		//发送消息到所有的用户
		virtual void	SendAllMessage(UserChatMsg*);
		//获取所有的用户的数量
		virtual int		GetChaterNumberInChannel();
		CHANNEL_ID		GetChannelId(){
			return this->_channel_id;
		};

		IChater* 		GetChaterByChaterId(CHATER_ID);

		const char*		GetChannelName(){return _channel_name;}

		void			UpdateTopMessage(UserChatMsg* msg){
			char* msg_buff = new char[msg->msg_size];
			memcpy(msg_buff,(char*)msg,msg->msg_size);
			if(_top_message_list.size() < 5){
				_top_message_list.push_back((UserChatMsg*)msg_buff);
			}else if(_top_message_list.size() == 5){
				MESSAGE_ITR itr = _top_message_list.begin();
				delete *itr;
				_top_message_list.pop_front();
				_top_message_list.push_back((UserChatMsg*)msg_buff);
			}
		}

		void	SendTopMessageToNewChater(IChater* iChater){
			MESSAGE_ITR itr = _top_message_list.begin();
			while(itr != _top_message_list.end()){
				iChater->SendCmdToClient((*itr));
				itr++;
			}
		}
	private:
		typedef std::map<CHATER_ID,IChater*> 			CHATER_MAP;
		typedef std::map<CHATER_ID,IChater*>::iterator 	CHATER_MAP_ITER;
		//频道的玩家表
		CHATER_MAP 									_chater_map;


		typedef std::list<UserChatMsg*>					MESSAGE_LIST;
		typedef std::list<UserChatMsg*>::iterator		MESSAGE_ITR;
		//频道的最近消息
		MESSAGE_LIST								_top_message_list;

		//频道的ID
		CHANNEL_ID									_channel_id;
		//频道的名称
		char										_channel_name[20];
		omg::MutexLock								_lock;
};

#endif /* CCHANNEL_H_ */
